<?php

namespace App\Http\Controllers\Commit;

use App\Repository\Commit\PageClassRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class PageClassController extends BaseController
{
  private $repoist;
  public function __construct(PageClassRepository $model)
  {
    $this->repoist = $model;
  }
  /**
   * @OA\Post(
   *     path="/api/v1/menu",
   *     tags={"后台菜单模块"},
   *     summary="添加",
   * @OA\Parameter(name="token", in="header", @OA\Schema(type="string")),
   *     @OA\Parameter(name="menu_tag", in="query", @OA\Schema(type="菜单名称 必填")),
   *     @OA\Parameter(name="path", in="query", @OA\Schema(type="路径 必填")),
   *     @OA\Parameter(name="parent_id", in="query", @OA\Schema(type="父级id 默认0 顶级菜单")),
   *     @OA\Parameter(name="grandfather", in="query", @OA\Schema(type="爷爷级id 默认 0")),
   *     @OA\Parameter(name="order_no", in="query", @OA\Schema(type="显示顺序")),
   *     @OA\Parameter(name="icon", in="query", @OA\Schema(type="图标")),
   *     @OA\Parameter(name="type", in="query", @OA\Schema(type="0 菜单 1动作 2报表菜单")),
   *     @OA\Parameter(name="hasFlag", in="query", @OA\Schema(type="是否显示 0 不显示 1 显示")),
   *     @OA\Parameter(name="window", in="query", @OA\Schema(type="窗口属性 默认 _self")),
   *     @OA\Parameter(name="action_tag", in="query", @OA\Schema(type="动作标识")),
   *     @OA\Parameter(name="action_menuid", in="query", @OA\Schema(type="属于某个菜单")),
   *     @OA\Response(response=200, description="  {code: int, info:string, data:[token=>1]}"
   *     )
   * )
   */
  public function store(Request $request)
  {
    $request->offsetSet('parent_id', $request->get('parentId'));
    $request->offsetSet('dept_name', $request->get('deptName'));
    $request->offsetSet('order_num', $request->get('orderNum'));
    $request->offsetSet('leader', $request->get('leader'));
    $request->offsetSet('dist_id', $request->get('distId'));
    $request->offsetSet('create_by', $request->get('createBy'));
    $request->offsetSet('create_time', $request->get('createTime'));
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('status'))) {
      $where['where']['status'] = $request->get('status');
    }
    if (!empty($request->get('deptName'))) {
      $where['like'] = ['dept_name' => $request->get('deptName')];
    }
    if ($request->user->role_id != 1) {
      $where['whereor'] = [
        'where' => [['shop_id', '=', $request->user->shop_id]],
        'whereor' => [['dept_id', '=', 1]],
      ];
      // $where['where']['shop_id'] = $request->user->shop_id;
    }
    // $where['where'][] = ['dept_id', '!=', 1];
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('deptId')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = [
      'dept_id' => $request->get('deptId'),
      'shop_id' => $request->user->shop_id,
    ];
    $request->offsetSet('parent_id', $request->get('parentId'));
    $request->offsetSet('dept_name', $request->get('deptName'));
    $request->offsetSet('order_num', $request->get('orderNum'));
    $request->offsetSet('leader', $request->get('leader'));
    $request->offsetSet('dist_id', $request->get('distId'));
    $request->offsetSet('create_by', $request->get('createBy'));
    $request->offsetSet('create_time', $request->get('createTime'));
    return $this->repoist->edit($where, $request);
  }
  // deptListExclude 不在这个部门下面的
  public function excludeIndex($id)
  {
    // $where[] = ['ancestors','like','%.'.$id.'.%'];
    $where['like'] = ['ancestors' => $id];
    $data = $this->repoist->index($where);
    $return = [];
    $array = [];
    if (!empty($data['data'])) {
      $array = array_column($data['data'], 'deptId');
    }
    $array[] = $id;
    $whereTwo['nIn'] = ['dept_id' => $array];
    $return = $this->repoist->index($whereTwo);
    return $return;
  }
  public function  show(Request $request, $id)
  {
    $where = ['dept_id' => $id, 'shop_id' => $request->user->shop_id,];
    //查询一条数据
    $return = $this->repoist->getOne($where);
    if (!empty($return)) {
      $return = usfirstArr($return);
      if (!empty($return['distId'])) {

        $res = explode(',', $return['distId']);
        $return['distId'] = [];
        $return['leader'] = !empty($return['leader'])?(int)$return['leader']:0;
        foreach ($res as $key => $v) {
          if(!empty($v)){
            $return['distId'][] = (int)$v;
          }
        }
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public  function deptTreeselect(Request $request)
  {
    $where = [
      'parent_id' => 0,
    ];
    $shop_id = $request->user->shop_id;
    // var_dump($shop_id);
    $data =  $this->repoist->treeSelect($where, [], $shop_id);
    return $this->returnMsg(200, '查询成功', $data);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['dept_name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'dept_id' => $id,
      'shop_id' => $request->user->shop_id,
    ];
    return $this->repoist->del($where);
  }
}
